#!/usr/bin/env python

#load smiles in table, identifier in supplier table

import string,os, sys, mysql
import MySQLdb, cPickle
import gzip 
from cStringIO import StringIO


username = sys.argv[1]
password = sys.argv[2]
stereo_table = sys.argv[3]  
stereo_id_field = sys.argv[4]
blob_field = sys.argv[5]

delete_db_file = sys.argv[6]

if delete_db_file == 'True':
	delete_db_file = True
else:
	delete_db_file = False


files = os.listdir('./')
files.sort()


#-------------------------------------
def store_molis(header,molis,label, stereo_table, stereo_id_field, blob_field):
	#write gzip file
	out_file = gzip.GzipFile('tmp.db.gz' , 'wb')
	out_file.write(header)
	out_file.write(molis)
	out_file.close()
	#print molis
	#print '------------------> end'

	#read gzip file
	
	zipped_file = open('tmp.db.gz' , 'rb')
	content = zipped_file.read()
	content = cPickle.dumps(content,2) #cPickle.dump converts data into binary structure
	#print '---------------------> new moli'
	#print label
	#print molis
	command = "update " + stereo_table + " set " + blob_field + " = %s where " + stereo_id_field + " = %s"
	#print command
	cursor.execute(command, (content,label))



#-------------------------------------
 
#conn=mysql.connect2server(password, username)  			  
			  
#cursor = conn.cursor ()

conn=mysql.connect2server(password, username,'purchasable') 
cursor = conn.cursor()


for file in files:
	if file[-3:] == '.db' or file[-4:] == '.bz2':
	 	if file[-4:] == '.bz2':
			command = 'bunzip2 ' + file
			print command
			os.system(command)
			file = file[0:-4] 
			print file

		database_file = open(file, 'r')
		print "processing file " + file
  		family = False
  		molecule = []
  		found = False
		old_label = ''
		molis = ''
		molis_dict = {}

		#get header
		header = database_file.readline()
		new_line = database_file.readline()
		while new_line.find('(') <> -1:
			header = header + new_line
			new_line = database_file.readline()

		#new line is family line, append to molecule
		family = True
		molecule.append(new_line)
		first = True #this flagg is needed to make sure that the first molecule per file does not get lost

 		for j in database_file.xreadlines():
    			if family:
				label = string.strip(j[47:57])
				j = j.replace(label, 'xxxxxxxx')
				label = label[:-2] #last two digest are for multiple ring alignments
      				family = False
				if first:
					old_label = label
					first = False 
				if label <> old_label:
					store_molis(header,molis,old_label, stereo_table, stereo_id_field, blob_field)
					molis = ''
					old_label = label

				
     			if j[0:6] =='Family':
       				family = True
				for k in molecule:
   					#print k
					molis = molis + k
				molecule = []
 
     			if j.find('0     0     0     0') == -1:
				#these lines would make dock stop
				molecule.append(j)
		#dont forget the last molcule
		for k in molecule:
	   		#print k
			molis = molis + k
 		#print molis
		store_molis(header,molis,old_label, stereo_table, stereo_id_field, blob_field)

		conn.commit() #submit after each file is loaded
		database_file.close()
		if delete_db_file:
			command = 'rm -f ' + file
			print command
			os.system(command)
conn.close ()



